	
Sample Perl Program

Sample Perl Program - Source Code

Our standard sample count-to-ten program in Perl:

#!/usr/local/bin/perl
		
########################################
#
#  Sample Perl Program - Counts To Ten
#
#  Copyright 2005 Andrew Eichstaedt
#  Eichstaedt Development Group
#  http://www.andrew-eichstaedt.com
#

print "Hello! I am a sample perl program";
print "that counts to ten.\n\n";

for($i = 1; $i <=10; $i++) {
    print "$i\n";
}

print "\nThanks for running me.\n";
          
General Notes on Perl

Perl is a mature, cross-platform, interpreted language with an exceptionally wide range of applications. Perl is an excellent, efficient choice for quick-and-dirty scripting, but is also fully scalable for robust, enterprise-grade systems. Perl supports disciplined object-orientied, self-documenting programming, but doesn't force any particular programming style.

Perl is widely used for web-based CGI applications. Extensive libraries supporting CGI and other Internet applications are available.

More perl programming resources are available below.

Sample Perl Program - Comments for Programmers

Perl is an interpreted language. You can typically execute a program with a command line invocation like:

> perl sample-perl-program.pl
The first line of the sample program is an exception. It uses the customary shebang syntax in Unix. It's useful if you're on a Unix system with the perl source file set as executable, allowing you to run the program by typing just its filename at the command line.

It's also useful in other situations, for example, when running a perl script as a CGI under Apache on Windows, or passing command-line switches to the perl interpreter. But it's not necessary if you're running the program invoking the perl interpreter explicitly from the command line as shown in the first example above.

Barring that exception, lines (or parts of lines) beginning with the pound sign (#) are treated as comments, as the next several lines in the source code show.

The two print statements that follow use C-like syntax and simply print the message to standard output. Using the C convention for escaped characters, "\n" is the Unix newline character.

The for loop after this also uses C-style syntax.

Unlike C, variables in perl are prefixed with a character that indicates their type. In the for loop, the index variable $i is a scalar, designated by the "$" prefix. Other prefixes indicate other types, like "@" for an array or "%" for a hash table, also known as an associative array.

CGI Script Example

Here's a CGI version:

#!/usr/local/bin/perl

########################################
#
#  Example Perl CGI Script-
#  Counts To Ten
#
#  Copyright 2005 Andrew Eichstaedt
#  Eichstaedt Development Group
#  http://www.andrew-eichstaedt.com
#

#  Print standard CGI header to stdout.
#  Add some simple HTML.
#
print "content-type: text/html \n\n";
print "<html>\n";
print "<head>\n";
print "<title>Example Perl Script - ";
print "Count to Ten</title>\n";
print "<body>\n";
print "<p>Hello! I am an example perl ";
print "script that counts to ten.</p>\n";

print "<p>\n";
for($i = 1; $i <=10; $i++) {
    print "$i<br>\n";
}
print "</p>\n";

print "<p>Thanks for running me.</p>\n";
print "</body>\n</html>\n";